/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "d3d_local.h"

IRSDriver	*CreateDriver( void ) {
	return new ED3DDriver();
}

/*-----------------------------------------------------------------------------
	D3D driver :
-----------------------------------------------------------------------------*/

//
//	ED3DDriver::ED3DDriver
//
ED3DDriver::ED3DDriver( void )
{
	LOG_INIT("Direct3D driver");
	
	InitDisplay();
	InitDirect3D();
	
	txm	=	new ED3DTXManager(this);
	rtm	=	new ED3DRTManager(this);
	vbm	=	new ED3DVBManager(this);
	fxm	=	new ED3DFXManager(this);
	
	ResetState();
}


//
//	ED3DDriver::~ED3DDriver
//
ED3DDriver::~ED3DDriver( void )
{
	LOG_SHUTDOWN("Direct3D driver");

	ShutdownDirect3D();
	ShutdownDisplay();
}


/*-----------------------------------------------------------------------------
	Frame stuff :
-----------------------------------------------------------------------------*/

//
//	ED3DDriver::ResetState
//
void ED3DDriver::ResetState( void )
{
	float zero  = 0;
	//	Config modulation params :
	HRCALL( d3ddev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE) );
	HRCALL( d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE) );

	//	Disable stuff :	
	HRCALL( d3ddev->SetRenderState( D3DRS_LIGHTING,				FALSE) );
	HRCALL( d3ddev->SetRenderState( D3DRS_FILLMODE,				D3DFILL_SOLID) );
	HRCALL( d3ddev->SetRenderState( D3DRS_CULLMODE,				D3DCULL_NONE) );
	
	//	Config blending :
	HRCALL( d3ddev->SetRenderState( D3DRS_ALPHABLENDENABLE,		FALSE) );
	HRCALL( d3ddev->SetRenderState( D3DRS_BLENDOP,				D3DBLENDOP_ADD) );
	
	//	Config Z-buffer :
	HRCALL( d3ddev->SetRenderState( D3DRS_ZWRITEENABLE,			D3DZB_TRUE ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_ZENABLE,				D3DZB_TRUE ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_ZFUNC,				D3DCMP_LESSEQUAL ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_DEPTHBIAS,			*(int*)&zero) );
	HRCALL( d3ddev->SetRenderState( D3DRS_SLOPESCALEDEPTHBIAS,	*(int*)&zero) );

	//	Config stencil :
	HRCALL( d3ddev->SetRenderState( D3DRS_STENCILENABLE, FALSE ) );

	uint mask =	 D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED;
	HRCALL( d3ddev->SetRenderState( D3DRS_COLORWRITEENABLE, mask ) );


	for (uint i=0; i<8; i++) {
		HRCALL( d3ddev->SetTexture(i, NULL) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 16) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR) );
		
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_ADDRESSW, D3DTADDRESS_WRAP) );
	}
}


//
//	ED3DDriver::CheckFeature
//
bool ED3DDriver::CheckFeature( ERSFeature feature )
{
	return true;
}


/*-----------------------------------------------------------------------------
	Frame stuff :
-----------------------------------------------------------------------------*/

//
//	ED3DDriver::BeginFrame
//
bool ED3DDriver::BeginFrame( void )
{
	if (!RestoreDevice()) {
		return false;
	}

    HRCALL( d3ddev->BeginScene() );

	HRCALL( d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, Vec4ToByte4(EVec4(0,0,0,0)), 1, 0 ) );

	ResetState();
	
	return true;
}


//
//	ED3DDriver::RestoreDevice
//
bool ED3DDriver::RestoreDevice( void )
{
	HRESULT hr      =       d3ddev->TestCooperativeLevel();

	if (SUCCEEDED(hr)) {
		return true;
	}

	if (hr==D3DERR_DEVICELOST) {
		LOG_WARNING("TestCooperativeLevel() : D3DERR_DEVICELOST");
		System()->Sleep(500);
		return false;

	} else if (hr==D3DERR_DEVICENOTRESET) {
		LOGF("TestCooperativeLevel() : D3DERR_DEVICENOTRESET");

		//
		//	restore resources :
		//
		//ReleaseTextures();

		d3ddev->Reset( &d3dpp );



		LOGF("Device has been restored");
		return true;

	} else {
		FATAL( "TestCooperativeLevel() failed" );
		return false;
	}
}


//
//	ED3DDriver::EndFrame
//
void ED3DDriver::EndFrame( void )
{
	//ScreenShot();
	
	HRCALL( d3ddev->EndScene() );
	
	HRESULT	hr	=	d3ddev->Present( NULL, NULL, NULL, NULL );
	
	if (hr==D3DERR_DEVICELOST) {
		LOG_WARNING("IDirect3DDevice9::Present() failed : D3DERR_DEVICELOST");
		return;
	}
    HRCALL( hr );
}





